package main;

import java.util.ArrayList;

import mediaitem.*;
import database.*;
import collection.*;

public class MediaLibrary {
	public static boolean createMediaItem(String itemName, 
										String description, String pathToFile){
		try{
			MediaItem item = new MediaItem(itemName, description, pathToFile);
			DatabaseSupport.storeMediaItem(item);	// store item into the database
		}catch(Exception e){
			return false;
		}

		return true;
	}
	
	public static boolean updateItemName(String itemName, String newName){
		MediaItem item;
		boolean returnResult;
		
		try{
			item = DatabaseSupport.getMediaItem(itemName);
		}
		catch(Exception e){
			return false;
		}
			
		returnResult = item.updateName(newName);
		
		// method only returns true if both updateName() and storeItem() are true
		return (returnResult & DatabaseSupport.storeMediaItem(item));	
	}

	public static boolean updateItemPath(String itemName, String newPath){
		MediaItem item;
		boolean returnResult;
		
		try{
			item = DatabaseSupport.getMediaItem(itemName);
		}
		catch(Exception e){
			return false;
		}
			
		returnResult = item.updatePath(newPath);
		
		// method only returns true if both updateName() and storeItem() are true
		return (returnResult & DatabaseSupport.storeMediaItem(item));	
	}
	
	public static boolean updateItemDescription(String itemName, String newDes){
		MediaItem item;
		boolean returnResult;
		
		try{
			item = DatabaseSupport.getMediaItem(itemName);
		}
		catch(Exception e){
			return false;
		}
			
		returnResult = item.updateDescription(newDes);
		
		// method only returns true if both updateName() and storeItem() are true
		return (returnResult & DatabaseSupport.storeMediaItem(item));	
	}
	
	public static boolean deleteMediaItem(String itemName){
		MediaItem item;
		
		try{
			item = DatabaseSupport.getMediaItem(itemName);
		}
		catch(Exception e){
			return false;
		}
		
		item.removeFromAll();	// remove from all collections
		return DatabaseSupport.removeMediaItem(itemName);
	}
	
	public static boolean createCollection(String name, ArrayList<Collection> parentCollections)
	{
		return Collection.createCollection(name, parentCollections);
	}

	public static boolean deleteCollection(String name)
	{
		return Collection.deleteCollection(name);
	}

	public static boolean updateCollectionName(String oldName, String newName)
	{
		return Collection.updateCollectionName(oldName, newName);
	}

	public static boolean updateCollectionParents(String name, ArrayList<Collection> newParents)
	{
		return Collection.updateCollectionParents(name, newParents);
	}
	
	public static boolean addItemToCollection(String collName, String itemName){
		Collection coll = DatabaseSupport.getCollection(collName);
		MediaItem item = DatabaseSupport.getMediaItem(itemName);
		
		coll.addMediaItem(item);
		item.addCollection(coll);
		DatabaseSupport.storeMediaItem(item);
		DatabaseSupport.putCollection(coll);
		
		return true; 
	}
	
	public static boolean removeItemFromCollection(String collName, String itemName){
		Collection coll = DatabaseSupport.getCollection(collName);
		MediaItem item = DatabaseSupport.getMediaItem(itemName);
		
		coll.removeMediaItem(item);
		item.removeCollection(coll);
		DatabaseSupport.storeMediaItem(item);
		DatabaseSupport.putCollection(coll);
		
		return true;
	}
	
	public static boolean addCollectionToCollection(String coll_1, String coll_2){
		Collection parent = DatabaseSupport.getCollection(coll_1);
		Collection child = DatabaseSupport.getCollection(coll_2);
		
		child.addParent(parent);
		parent.setChild(child);
		
		DatabaseSupport.putCollection(parent);
		DatabaseSupport.putCollection(child);
		
		return true;
	}
	
	public static boolean removeCollectionFromCollection(String coll_1, String coll_2){
		Collection parent = DatabaseSupport.getCollection(coll_1);
		Collection child = DatabaseSupport.getCollection(coll_2);
		
		child.removeFrom(parent);
		parent.removeChild(child);
		
		DatabaseSupport.putCollection(parent);
		DatabaseSupport.putCollection(child);
		
		return true;
	}
}

